//
//  VISTAMagaDetailVC.m
//  看杂志
//
//  Created by zcj on 15-12-14.
//  Copyright (c) 2015年 郑朝军. All rights reserved.
//

#import "VISTAMagaDetailVC.h"
#import "SelectFindHttp.h"
#import "MJRefresh.h"
#import "VISTAInfoDetailVC.h"
#import "Grouble.h"
#import "VISTAMagaDetailModel.h"
#import "CatagerName.h"
#import "VISTAMagaDetailModel.h"
#import "VISTAMagaDetailCell.h"

@interface VISTAMagaDetailVC ()
{
    UITableView *_tableView;
    BOOL isPull;
    
}
//@property (nonatomic, strong)NSMutableArray *dataSourceArray;
@property (nonatomic, strong)NSMutableArray *catName;
@property (nonatomic, strong)NSMutableArray *list;
@property(nonatomic, strong)UITableView *tableView;
@end

@implementation VISTAMagaDetailVC
- (NSMutableArray *)catName
{
    if (_catName == nil) {
        _catName = [NSMutableArray array];
    }
    return _catName;
}
- (NSMutableArray *)list
{
    if (_list == nil) {
        _list = [NSMutableArray array];
    }
    return _list;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setTableView];
    [self setRush];
    NSLog(@"%@",_textId);
    if (!_textId) {
        [self back];
        [self configViewAndNavi];
        
        return ;
    }
    self.view.backgroundColor = [UIColor whiteColor];
    NSLog(@"textID %@",_textId);
    NSLog(@"定制视图和导航栏");
    [self configViewAndNavi];
    
}
// 下拉刷新
- (void)setRush
{
    [self.tableView addGifHeaderWithRefreshingTarget:self refreshingAction:@selector(refreshData)];
    NSMutableArray * idleImages = [NSMutableArray array];
    for (NSUInteger i = 1; i<=60; ++i) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"dropdown_anim__000%zd",i]];
        [idleImages addObject:image];
    }
    [self.tableView.gifHeader setImages:idleImages forState:MJRefreshHeaderStateIdle];
    //设置即将刷新状态的动画图片
    NSMutableArray *refreshingImages = [NSMutableArray array];
    for (NSInteger i = 1; i<=3; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"dropdown_loading_0%zd",i]];
        [refreshingImages addObject:image];
    }
    [self.tableView.gifHeader setImages:refreshingImages forState:MJRefreshHeaderStatePulling];
    
    //设置正在刷新是的动画图片
    [self.tableView.gifHeader setImages:refreshingImages forState:MJRefreshHeaderStateRefreshing];
    
    //马上进入刷新状态
    [self.tableView.gifHeader beginRefreshing];
    
}

- (void)setTableView
{
    //ui
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
}

- (void)refreshData
{
      NSString *path = [NSString stringWithFormat:@"http://ktx.cms.palmtrends.com/api_v2.php?action=get_mags_detail&sa=&uid=10067567&mobile=iphone5&offset=0&count=1000&magid=%@&e=40dab97d773e7860febfc897c04824e2&uid=10067567&pid=10053&mobile=iphone5&platform=i",_textId];
    NSLog(@"%@",path);
    [SelectFindHttp postWirhUrl:path parms:nil success:^(id json) {
        [self.catName removeAllObjects];
        NSArray * cates = json[@"cats"];
        for (NSDictionary * dict in cates) {
            CatagerName * cateName = [CatagerName objectWithKeyValues:dict];
           // NSArray * model =  [dict objectForKey:@"list"];
//            VISTAMagaDetailModel * modelTwo = model[0];
//            for (NSDictionary * dictTwo in model) {
//                modelTwo.ID = [dictTwo objectForKey:@"id"];
//            }
//            ZCJLog(@"%@",modelTwo.ID);
            [self.catName addObject:cateName];
        }
        [self.tableView reloadData];
        [self.tableView.gifHeader endRefreshing];
        [MBProgressHUD showSuccess:@"成功加载"];
    } failture:^(id error) {
        ZCJLog(@"%@",error);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
/**
 *  设置viewController背景图和定制导航栏
 */
- (void)configViewAndNavi
{
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"NAVI"] forBarMetrics:UIBarMetricsDefault];
    //定制导航栏左按钮
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 50, 32);
    [leftButton setImage:[UIImage imageNamed:@"返回_1"] forState:UIControlStateNormal];
    [leftButton setImage:[UIImage imageNamed:@"返回_2"] forState:UIControlStateHighlighted];
    [leftButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftItem;
    //导航栏标题
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 50, 30)];
    titleLabel.text = @"资讯";
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLabel;
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"资讯背景底"]];
}
- (void)back
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark TableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.catName.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    VISTAMagaDetailCell *cell = [VISTAMagaDetailCell cellWithTableView:tableView];
    [cell fillCellWithModel:self.catName[indexPath.row]];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    VISTAInfoDetailVC *textVC = [[VISTAInfoDetailVC alloc]init];
    textVC.dataSourceArray = _list;
    textVC.indexPathRow = indexPath.row;
    textVC.flage = 1;
    [self.navigationController pushViewController:textVC animated:YES];
    
}

@end
